﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YJKRevitCode.FormUtility.ModelessFormControlSuite.ModelLayer;

namespace YJKRevitCode.FormUtility.ModelessFormControlSuite.ControlLayer
{
	public class BaseLoopMangerSwitch
	{
		public BaseLoopMangerSwitch(UIDocument inputUiDoc, IModelessFormSwitch inputForm, IOperateStackFactory inputFactory, IWin32Window inputParentHandle = null, bool ifFinishBreak = false)
		{
			if (inputUiDoc == null || inputForm == null || inputFactory == null)
			{
				throw new System.ArgumentNullException();
			}
			this.m_uiDoc = inputUiDoc;
			this.m_Doc = this.m_uiDoc.Document;
			this.m_useForm = inputForm;
			this.m_useFacory = inputFactory;
			this.m_useParentHandle = inputParentHandle;
			this.m_bIfFinishBreak = ifFinishBreak;
		}

		public virtual void DoLoopWork()
		{
			this.m_useForm.ShowForm(this.m_useParentHandle);
			int nowCaseIndex = this.m_useForm.NowCaseIndex;
			this.m_usePickObjectMethod = this.m_useFacory.GetAPickMethod(nowCaseIndex);
			IOperateStack operateStack = this.m_useFacory.GetAStack(nowCaseIndex);
			for (;;)
			{
				this.m_useForm.ResetContinueBreak();
				try
				{
					object input = this.m_usePickObjectMethod();
					operateStack.AddToStack(input);
					if (operateStack.TryFinishStack())
					{
						this.m_lstAllOperateStack.Add(operateStack);
						nowCaseIndex = this.m_useForm.NowCaseIndex;
						operateStack = this.m_useFacory.GetAStack(nowCaseIndex);
					}
					if (!this.m_bIfFinishBreak)
					{
						continue;
					}
					this.m_useForm.FinishWorkBreakMethod();
				}
				catch (Exception ex)
				{
                    if (!(ex is System.OperationCanceledException) && !(ex is EscException))
					{
						throw ex;
					}
					bool ifContinue = this.m_useForm.IfContinue;
					bool ifBreak = this.m_useForm.IfBreak;
					if (ifContinue)
					{
						operateStack.ClearStack();
						operateStack = null;
						nowCaseIndex = this.m_useForm.NowCaseIndex;
						operateStack = this.m_useFacory.GetAStack(nowCaseIndex);
						this.m_usePickObjectMethod = this.m_useFacory.GetAPickMethod(nowCaseIndex);
						this.DoBeforeContinue();
						continue;
					}
					if (!ifBreak)
					{
						continue;
					}
					operateStack.ClearStack();
					operateStack = null;
					this.DoBeforeClose();
					if (this.m_useForm.IfManualEsc)
					{
						this.m_useForm.CloseForm();
					}
				}
				break;
			}
			this.DoAfterWhile();
		}

		protected virtual void DoAfterWhile()
		{
		}

		protected virtual void DoBeforeClose()
		{
		}

		protected virtual void DoBeforeContinue()
		{
		}

		private Document m_Doc;

		private UIDocument m_uiDoc;

		protected IModelessFormSwitch m_useForm;

		protected IOperateStackFactory m_useFacory;

		protected IWin32Window m_useParentHandle;

		protected PickObjectMethod m_usePickObjectMethod;

		protected List<IOperateStack> m_lstAllOperateStack = new List<IOperateStack>();

		protected bool m_bIfFinishBreak;
	}
}
